#pragma once
#include <Arduino.h>
#include "wire.hpp"

namespace ganix
{
    class BH1750 {
    private:
        const uint8_t addr = 0b0100011;
        const uint8_t wake = 0x10;
        const float measurement_accuracy = 1.2;
        const int recv_size = 2;
    public:
        BH1750() {
        }

        void init() {
            wire::i2c_init();
        }

        int recv() {
            wire::i2c_send(addr, &wake);
            delay(200);
            uint8_t buf[recv_size];
            if (wire::i2c_recv(addr, buf, recv_size) == recv_size) {
                return (buf[0]<<8|buf[1]); 
            }
            return -1;
        }

        float recv_lumen() {
            int x = recv();
            return x == -1 ? -1 : x / measurement_accuracy;
        }
    };

    namespace singleton
    {
        BH1750 _bh1750;

        void init_bh1750() {
            _bh1750.init();
        }

        BH1750& bh1750() {
            return _bh1750;
        }
    } // namespace singleton
    
} // namespace ganix

